package cn.topevery.garden.dal.logic.warning;

import cn.topevery.garden.dal.logic.base.WarningBase;
import cn.topevery.garden.entity.po.IotDeviceIrrigationLog;
import cn.topevery.garden.framework.util.WarningConfigUtil;
import hw.topevery.basis.entity.po.SysClass;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：peng.tian
 * @date ：Created in 2021/9/26 11:37
 * @description：灌溉预警处理
 */
@Component
public class IotDeviceIrrigationWarning extends WarningBase {
    @Override
    public String getClassId() {
        return "2E2BEAAA-D00B-46B0-A4FC-DCC2FB59F935";
    }

    @Override
    public String getFacilitiesType() {
        return "607f2817-192c-4630-be88-ec1d503d700e";
    }

    @Override
    public String getWarningType() {
        return "2D37C78E-6748-4355-8672-570243957EB7";
    }



    public void check(IotDeviceIrrigationLog data) {
        try {
            Map<String,String> flagMap= new HashMap() {
                {
                    put("土壤墒情1传感器",data.getA01097flag());
                    put("土壤墒情2传感器",data.getA01098flag());
                    put("土壤墒情3传感器",data.getA01099flag());
                    put("第1路喷灌状态",data.getG10001flag());
                    put("第2路喷灌状态",data.getG10002flag());
                    put("第3路喷灌状态",data.getG10003flag());
                }
            };

            List<SysClass> sysClassList = this.getWarningItems();
            Map filedsInfo = this.getFiledsInfo(data);
            StringBuilder warningDesc = new StringBuilder();
            if (sysClassList != null && sysClassList.size() > 0) {
                sysClassList.forEach(f -> {
                    this.setSmallClassId(f.classId);

                    Double threshold = this.getValue(f.classId);

                    if(f.code.equals("allFlag")&&threshold.equals(1)){
                        flagMap.keySet().forEach(key->{
                            if(flagMap.get(key).equals("D")){
                                warningDesc.append(key+"异常\n");
                            }
                        });
                    }
                    else {
                        Map<String, String> columnMap = new HashMap<>();
                        if (f.code.contains("/")) {
                            String[] keyVals = f.code.split("/");
                            for (int i = 0; i < keyVals.length; i++) {
                                String[] keyval = keyVals[i].split(":");
                                columnMap.put(keyval[1]+ f.name, keyval[0]);
                            }
                        } else {
                            columnMap.put(f.name, f.code);
                        }

                        columnMap.keySet().forEach(name -> {
                            String code = columnMap.get(name);
                            if (filedsInfo.containsKey(code)) {
                                Double value = Double.parseDouble(filedsInfo.get(code).toString());
                                try {
                                    if(WarningConfigUtil.Judge(value,threshold,f.remark)){
                                        warningDesc.append(name + ",当前值:" + value.toString() + ",阈值:" + threshold.toString() + "\n");
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                });
                if(warningDesc.toString().length()>0){
                    this.sendWarning(data.getDeviceCode(),warningDesc.toString(), data.getId());
                }
            }
        }
        catch (Exception ex){
            ex.printStackTrace();
            System.out.println("智能喷灌预警检测异常!");
        }
    }
}
